# require File.dirname(__FILE__) + "/openid_engine"
# require File.dirname(__FILE__) + '/rp'
require "rp"

module OpenidEngine::ActsAsRp
  include OpenidEngine
  protected
  
  # ERROR_MESSAGES = {
  #   :missing      => "Sorry, the OpenID server couldn't be found",
  #   :canceled     => "OpenID verification was canceled",
  #   :failed       => "Sorry, the OpenID verification failed",
  #   :setup_needed => "OpenID verification needs setup"
  # }
  
  def openid_request?
    (params[:openid_identifier] || params['openid.ns']) ? true : false
  end
  
  # def requested_url
  #   "#{request.protocol + request.host_with_port + request.relative_url_root + request.path}"
  # end
  
  # def request_openid_authentication(request_config)
  #   identifier = normalize(params[:openid_identifier])
  #   discover(identifier) do |discovered_information|
  #     puts discovered_information
  #   end
  # end
  
  # ruby-openid based, not to be used anymore
  # def request_openid_authentication_old
  #   
  #   identifier = OpenidEngine::Identifier.new(params[:openid_identifier])
  #   store = OpenID::Store::Filesystem.new(conf[:store_path])
  #   assoc_negotiator = OpenID::AssociationNegotiator.new([['HMAC-SHA256', 'DH-SHA256']])
  #           
  #   begin      
  #     yadis_url, services = OpenID.discover(identifier.uri)
  #     service = services.first
  # 
  #     assoc = OpenID::Consumer::AssociationManager.new(
  #       store, service.server_url, service.compatibility_mode, assoc_negotiator).get_association
  #     checkid_request = OpenID::Consumer::CheckIDRequest.new(assoc, service)
  #     redirect_to checkid_request.redirect_url(conf[:realm], conf[:return_to])
  #     
  #   rescue OpenID::OpenIDError, Timeout::Error
  #     yield ERROR_MESSAGES[:missing], identifier.uri # TODO: fix me
  #   end

    # session('last_requested_endpoint', endpoint)
    
    # start seq
    # begin
    #   oidreq = consumer.begin(params[:openid_identifier])
    # rescue OpenID::OpenIDError => e
    #   flash[:error] = "Discovery failed for #{params[:openid_identifier]}: #{e}"
    #   redirect_to :action => 'index'
    #   return
    # end
    # if params[:use_sreg]
    #   sregreq = OpenID::SReg::Request.new
    #   # required fields
    #   sregreq.request_fields(['email'], true)
    #   # optional fields
    #   sregreq.request_fields(['screenname'], false)
    #   oidreq.add_extension(sregreq)
    #   oidreq.return_to_args['did_sreg'] = 'y'
    # end
    # if params[:use_pape]
    #   papereq = OpenID::PAPE::Request.new
    #   papereq.add_policy_uri(OpenID::PAPE::AUTH_PHISHING_RESISTANT)
    #   papereq.max_auth_age = 2*60*60
    #   oidreq.add_extension(papereq)
    #   oidreq.return_to_args['did_pape'] = 'y'
    # end
    # if params[:force_post]
    #   oidreq.return_to_args['force_post']='x'*2048
    # end
    # return_to = complete_session_url
    # realm = session_url
    # 
    # if oidreq.send_redirect?(realm, return_to, params[:immediate])
    #   redirect_to oidreq.redirect_url(realm, return_to, params[:immediate])
    # else
    #   @form_text = oidreq.form_markup(realm, return_to, params[:immediate], {'id' => 'openid_form'})
    # end
  
end
